<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane label="工具面板" class="form-tabs-tab-pane">
            <!-- 表名 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.table')" prop="table">
                  <el-input
                    v-model.trim="form.table"
                    :placeholder="$t('codeMaker.table_tips')"
                    class="input-with-select"
                  >
                    <el-select
                      slot="prepend"
                      v-model="tableSelected"
                      :placeholder="$t('common.pleaseSelect')+$t('common.table')"
                      style="width: 150px"
                      clearable
                      @change="handleTableSelectChange"
                    >
                      <el-option label="info" value="info" />
                      <el-option label="admin_log" value="admin_log" />
                    </el-select>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 模型 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.model')" prop="model">
                  <el-input
                    v-model="form.model"
                    :placeholder="$t('codeMaker.model_tips')"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 模块 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.module')"
                  prop="module"
                >
                  <el-input
                    v-model="form.module"
                    :placeholder="$t('codeMaker.module_tips')"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button type="primary" @click="onSubmit">{{ $t('common.make') }}</el-button>
              <el-button @click="resetForm">{{ $t('common.reset') }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
// export
export default {
  name: 'CodeMakerEdit',
  components: {},
  data() {
    const validateTable = (rule, value, callback) => {
      if (!this.form.table && !this.tableSelected) {
        callback(new Error('请选择或填写表名'))
      } else {
        callback()
      }
    }
    return {
      form: {
        table: '',
        model: '',
        module: ''
      },
      tableSelected: undefined,
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        table: [
          { validator: validateTable, trigger: 'blur' },
          { pattern: /^[^_][a-zA-Z_]*[^_]$/, message: '请填写正确的表名' }
        ]
      },
      // 表列表
      tableList: [],
      // 防抖定时器
      debounce_timer: null
    }
  },
  computed: {},
  // 监听输入变化
  watch: {
    'form.table': {
      deep: true,
      handler(newVal, oldVal) {
        if (this.debounce_timer !== null) {
          clearTimeout(this.debounce_timer)
        }
        this.debounce_timer = setTimeout(() => {
          this.tableChange(newVal)
        }, 500)
      }
    }
  },
  created() {
    this.getTables()
  },
  methods: {
    /** *********************************************** 表单相关 开始 ******************************************************/
    // 获取表列表
    async getTables() {
      // 请求接口
    },
    // 提交表单
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          this.$message.error('提交失败!')
          return false
        }
        this.$confirm('是否确认生成?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            // 请求接口
            this.$message.success('生成成功!')
          })
          .catch(() => {})
      })
    },
    // 取消/返回
    onCancel() {
      // 删除当前页标签并返回上一页
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
      // this.$message.info('已取消！')
    },
    // 表单重置
    resetForm() {
      this.$refs['form'].resetFields()
    },
    // 选中表事件
    handleTableSelectChange(v) {
      if (v) {
        this.tableChange(v)
      }
    },
    tableChange(v) {
      this.form.table = v
      const transferMArr = []
      v.split('_').forEach((element) => {
        transferMArr.push(element.charAt(0).toUpperCase() + element.slice(1))
      })
      this.form.module = this.form.model = transferMArr.join('')
      this.$refs['form'].clearValidate('table')
    }
    /** *********************************************** 表单相关 结束 ******************************************************/
  }
}
</script>

<style scoped lang="scss">
</style>
